<?php

namespace app\index\validate;

use app\index\model\CertUseModel;
use think\Validate;

class CertUse extends Validate
{

    protected $rule = [
        'id' => 'require|number',
        'type' => 'require',
        'apply_type' => 'require',
        'list' => 'require|checkList|checkExists',
        'xxx' => 'xxx'
    ];

    protected $message = [
        'id' => '网络错误',
        'type' => '请选择用证类型',
        'apply_type' => '请选择申请类型',
        'list' => '请填写明细',
    ];

    protected $scene = [
        'add' => ['type','apply_type','list'],
        'edit' => ['id','type','apply_type','list'],
    ];

    function checkList($value, $rule, $data){
        if(count($value) === 1) {
            return '请填写明细';
        }
        return true;
    }

    function checkExists($value, $rule, $data){
        if($data['apply_type'] == 2){
            $check = CertUseModel::where(['deleted_user'=>0,'enginid'=>$data['enginid'],'type'=>$data['type'],'apply_type'=>2,'isturn'=>1,
                'status'=>'0'])->find();
            if($check){
                return '该项目变更申请已存在，请勿重复添加';
            }
        }
        if($data['apply_type'] == 1){
            $check = CertUseModel::where(['deleted_user'=>0,'enginid'=>$data['enginid'],'type'=>$data['type'],'apply_type'=>1,'isturn'=>1])
                ->where('status','in','0,1')->find();
            if($check){
                return '该项目首次申请 在申请中或已存在，请勿重复添加';
            }
        }
        return true;
    }


}

